package com.jianbing.north.controller;

import com.github.pagehelper.PageInfo;
import com.jianbing.common.core.controller.BaseController;
import com.jianbing.common.core.domain.AjaxResult;
import com.jianbing.common.core.page.TableDataInfo;
import com.jianbing.north.domain.*;
import com.jianbing.north.mapper.NorthP3DailyMapper;
import com.jianbing.north.service.INorthAnswerRecordService;
import com.jianbing.north.service.INorthPart3RecordService;
import com.jianbing.north.service.INorthQuesStemService;
import com.jianbing.north.service.INorthUserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.*;


/**
 * @author zheng
 */
@RestController
@RequestMapping("/north/data")
public class NorthDataController extends BaseController {
    @Resource
    private INorthAnswerRecordService northAnswerRecordService;
    @Resource
    private INorthPart3RecordService northPart3RecordService;
    @Resource
    private INorthUserInfoService northUserInfoService;
    @Resource
    private INorthQuesStemService northQuesStemService;

    @Resource
    private NorthP3DailyMapper northP3DailyMapper;

    private SimpleDateFormat outputFormatter = new SimpleDateFormat("yyyy-MM-dd");

    private Logger logger = LoggerFactory.getLogger(getClass());

    @PostMapping("/p1")
    public TableDataInfo getP1(NorthUserRecordDTO northUserRecordDTO)
    {
        startPage();
        List<NorthUserRecordDTO> list = northAnswerRecordService.selectNorthAnswerRecordSummaryList(northUserRecordDTO.getUserId(),1L);
        return getDataTable(list);
    }

    @PostMapping("/p2")
    public TableDataInfo getP2(NorthUserRecordDTO northUserRecordDTO)
    {
        startPage();
        List<NorthUserRecordDTO> list = northAnswerRecordService.selectNorthAnswerRecordSummaryList(northUserRecordDTO.getUserId(),2L);
        return getDataTable(list);
    }

    @PostMapping("/p3")
    public AjaxResult getP3(NorthPart3Record northPart3Record)
    {
        AjaxResult ajaxResult = new AjaxResult();
        List<NorthP3Record> p3Records = new ArrayList<>();
        startPage();
        List<NorthPart3Record> recordList = northPart3RecordService.selectP3(northPart3Record);
        for(NorthPart3Record record:recordList){
            p3Records.add(part3RecordToP3Record(record));
        }
        ajaxResult.put("total",new PageInfo(recordList).getTotal());
        ajaxResult.put("rows",p3Records);
        return ajaxResult;
    }

    @PostMapping("/fraction")
    public TableDataInfo getFraction(@RequestParam(required = false)Long userId)
    {
        startPage();
        NorthUserInfo northUserInfo = new NorthUserInfo();
        northUserInfo.setUserId(userId);
        List<NorthUserInfo> list = northUserInfoService.selectNorthUserInfoList(northUserInfo);
        for(NorthUserInfo info:list){
            info.setAllFraction(info.getP1Fraction()+info.getP2Fraction());
        }
        return getDataTable(list);
    }

    @PostMapping("/p3/daily")
    public TableDataInfo getP3Daily(NorthP3DailyDto northP3DailyDto)
    {
        startPage();
        List<NorthP3DailyDto> list = northP3DailyMapper.list(northP3DailyDto);
        return getDataTable(list);
    }

    private NorthP3Record part3RecordToP3Record(NorthPart3Record record){
        NorthP3Record p3Record = new NorthP3Record();
        p3Record.setDialogueId(record.getDialogueId());
        p3Record.setUserId(record.getUserId());
        String[] strings = record.getPath().split(",");
        for(String s:strings){
            String[] s1 = s.split(" ");
            if(s1[0].equals("1")){
                p3Record.setP1(s1[1]);
            }else if (s1[0].equals("2")){
                p3Record.setP2(s1[1]);
            }else if (s1[0].equals("3")){
                p3Record.setP3(s1[1]);
            }else if (s1[0].equals("4")){
                p3Record.setP4(s1[1]);
            }else if (s1[0].equals("5")){
                p3Record.setP5(s1[1]);
            }
        }
        String[] strings2 = record.getIsCorrect().split(",");
        for(String s:strings2){
            String[] s1 = s.split(" ");
            if(s1[0].equals("1")){
                p3Record.setP1IsCorrect(s1[1]);
            }else if (s1[0].equals("2")){
                p3Record.setP2IsCorrect(s1[1]);
            }else if (s1[0].equals("3")){
                p3Record.setP3IsCorrect(s1[1]);
            }else if (s1[0].equals("4")){
                p3Record.setP4IsCorrect(s1[1]);
            }else if (s1[0].equals("5")){
                p3Record.setP5IsCorrect(s1[1]);
            }
        }
        p3Record.setPlayDay(outputFormatter.format(record.getInsertTime()));
        p3Record.setBeginTime(record.getBeginTime());
        p3Record.setEndTime(record.getEndTime());
        p3Record.setPlayDuration(longToString(getDuration(record.getBeginTime(),record.getEndTime(),0L)));
        if(record.getDialogueId()!=null){
            p3Record.setQues(northQuesStemService.selestQuesByDialogueId(record.getDialogueId()));
        }
        return p3Record;
    }

    private Long getDuration(Date begin,Date end,Long playedTime){
        String timeFormat = null;
        Long time = 0L;
        if(begin!=null && end!=null){
            Duration duration = Duration.between(begin.toInstant(),end.toInstant());
            time = duration.getSeconds();
            if(playedTime!=null){
                time += playedTime;
            }
        }
        return time;
    }

    private String longToString(Long time){
        return String.format("%02d:%02d:%02d", time / 3600, (time % 3600) / 60, time % 60);
    }
}
